home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / hippo / x11test.c < prev   
Encoding:
C/C++ Source or Header  |  1992-04-28  |  6.0 KB  |  204 lines

  1. #include <X11/Xlib.h>
  2. #define __TYPES
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "hippo.h"
  7.  
  8. static int width=600, height=600;
  9.  
  10. static void doHandleEvents();
  11. int main()
  12. {
  13.      ntuple *nt_list;
  14.      display d_list[9];
  15.      FILE *outfile;
  16.      int nnt,i;
  17.      
  18.      /*
  19.       * read in example ntuple
  20.       */
  21.      if (h_read("example.hippo",d_list,&nt_list) != 0)
  22.      {
  23.       fprintf(stderr,"Error reading file example.hippo\n");
  24.       exit(1);
  25.      }
  26.  
  27.      /*
  28.       * check a few things
  29.       */
  30.      for (nnt=0; nt_list[nnt] != NULL; nnt++);
  31.      if (nnt != 1)
  32.      {
  33.       fprintf(stderr,"There are %d ntuples in the file\n",nnt);
  34.       fprintf(stderr,"  There should only be 1!\n");
  35.       exit(1);
  36.      }
  37.      
  38.      /*
  39.       * create 9 displays
  40.       */
  41.  
  42.      if ((d_list[0] = h_newDisp(HISTOGRAM)) == NULL)
  43.      {
  44.       fprintf(stderr,"Error creating display\n");
  45.       exit(1);
  46.      }
  47.      if ((d_list[1] = h_newDisp(HISTOGRAM)) == NULL)
  48.      {
  49.       fprintf(stderr,"Error creating display\n");
  50.       exit(1);
  51.      }
  52.      if ((d_list[2] = h_newDisp(HISTOGRAM)) == NULL)
  53.      {
  54.       fprintf(stderr,"Error creating display\n");
  55.       exit(1);
  56.      }
  57.      if ((d_list[3] = h_newDisp(HISTOGRAM)) == NULL)
  58.      {
  59.       fprintf(stderr,"Error creating display\n");
  60.       exit(1);
  61.      }
  62.      if ((d_list[4] = h_newDisp(HISTOGRAM)) == NULL)
  63.      {
  64.       fprintf(stderr,"Error creating display\n");
  65.       exit(1);
  66.      }
  67.      if ((d_list[5] = h_newDisp(SCATTERPLOT)) == NULL)
  68.      {
  69.       fprintf(stderr,"Error creating display\n");
  70.       exit(1);
  71.      }
  72.      if ((d_list[6] = h_newDisp(COLORPLOT)) == NULL)
  73.      {
  74.       fprintf(stderr,"Error creating display\n");
  75.       exit(1);
  76.      }
  77.      if ((d_list[7] = h_newDisp(COLORPLOT)) == NULL)
  78.      {
  79.       fprintf(stderr,"Error creating display\n");
  80.       exit(1);
  81.      }
  82.      if ((d_list[8] = h_newDisp(LEGOPLOT)) == NULL)
  83.      {
  84.       fprintf(stderr,"Error creating display\n");
  85.       exit(1);
  86.      }
  87.      
  88.      /*
  89.       * set display attributes
  90.       */
  91.      for (i=0; i<9; i++)
  92.        {
  93.          h_bindNtuple( d_list[i], nt_list[0] );
  94.          h_bind(d_list[i],XAXIS, 3);
  95.          h_bind(d_list[i],YAXIS, 10);
  96.        }
  97.  
  98.      h_setDrawType(d_list[1],ERRBAR);
  99.      h_setDrawType(d_list[2],POINT+LINE);
  100.      h_setDrawType(d_list[3],POINT+LINE);
  101.      h_setPlotSym(d_list[3],TIMES);
  102.      h_setLineStyle(d_list[3],DOTDASH);
  103.      h_bind(d_list[3],WEIGHT,10);
  104.  
  105.      h_setDrawType(d_list[7],COLOR);
  106.  
  107.      h_setPlotDrvr(X11PLOT); 
  108.     /*
  109.       * now create X output of display
  110.       * we make one large window, then six sub-windows, one for each plot
  111.       *
  112.       */ 
  113.      
  114.      {
  115.        Display *disp = XOpenDisplay(NULL);
  116.        Screen  *scrn = XDefaultScreenOfDisplay(disp);
  117.        GC       gc   = DefaultGCOfScreen(scrn);
  118.        XSetWindowAttributes xswa;
  119.        Window  main, wind[10];
  120.       
  121.        xswa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
  122.        xswa.background_pixel = WhitePixelOfScreen(scrn);
  123.  
  124.        main = XCreateWindow(disp,DefaultRootWindow(disp),
  125.                             10,10,width,height,0,
  126.                             DefaultDepthOfScreen(scrn), InputOutput, 
  127.                             DefaultVisualOfScreen(scrn), 
  128.                             CWEventMask | CWBackPixel, &xswa);
  129.                                         
  130.        xswa.event_mask = ExposureMask | ButtonPressMask;
  131.        for (i=0; i<9; i++)
  132.          {
  133.            wind[i] = XCreateWindow(disp,main,
  134.                                    (i-i/3*3)*200,(i/3)*200,200,200,0,
  135.                                    DefaultDepthOfScreen(scrn), InputOutput, 
  136.                                    DefaultVisualOfScreen(scrn), 
  137.                                    CWEventMask | CWBackPixel, &xswa);
  138.            XMapWindow(disp,wind[i]);
  139.          }
  140.  
  141.        XStoreName(disp, main, "X-Hippo example display");
  142.        XMapWindow(disp,main);
  143.        wind[9]=main;                      
  144.        doHandleEvents(d_list,disp,scrn,wind,gc);
  145.      }
  146.  
  147. }
  148. static void doHandleEvents(display disp[],Display *dpy,Screen *scrn,Window wind[],GC gc)
  149. {
  150.     XEvent event;
  151.     XAnyEvent *any = (XAnyEvent *) &event;
  152.     XExposeEvent *expose = (XExposeEvent *) &event;
  153.     XConfigureEvent *config = (XConfigureEvent *) &event;
  154.     int i;
  155.  
  156.     for ( ; ; ) {
  157.         XNextEvent(dpy, &event);
  158.         
  159.         for (i=0; i<9; i++) if (any->window==wind[i]) break;
  160.  
  161.         switch (event.type) {
  162.             case Expose:       if (expose->count==0)
  163.                                  {
  164.                                    printf("expose %d\n",i);
  165.                                    if (i<9)
  166.                                      h_plot(disp[i],dpy,scrn,wind[i],gc);
  167.                                    if (i==4)
  168.                                      {
  169.                                        h_shade(disp[i],0. ,40. );   
  170.                                        h_shade(disp[i],60.,100.);   
  171.                                      }
  172.                                  } 
  173.                                break;
  174.             case ButtonPress:  printf("Button press in window %d\n",i);
  175.                                break;
  176.  
  177.             case ConfigureNotify:
  178.  
  179.                                if (config->width != width ||
  180.                                    config->height != height)
  181.                                  {
  182.                                    int x,y,w,h;
  183.  
  184.                                    height = config->height;
  185.                                    width  = config->width;
  186.  
  187.                                    w = width/3;
  188.                                    h = height/3;
  189.                                    x = 0;
  190.                                    y = 0;
  191.                                  
  192.                                    for (i=0; i<9; i++)
  193.                                      {
  194.                                        XMoveResizeWindow(dpy,wind[i],x,y,w,h);
  195.                                        x += w;
  196.                                        if (i == 2 || i == 5)
  197.                                          { x = 0; y += h; }
  198.                                      }
  199.                                  }
  200.                                break;
  201.         }
  202.     }
  203. }
  204.